<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cache;
use think\Request;

/**
 * 首页接口
 */
class Banner extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct()
    {
        parent::__construct();
        $this->model = new \app\common\model\Banner;
    }

    /**
     * 获取轮播图
     *
     */
    public function index()
    {
        if (Cache::has('banner')){
            $data = Cache::get('banner');
        }else{
            $map = [
                'switch' => 1
            ];

            //只显示5条显示的数据
            $data = $this->model
                ->field('id,image,weigh')
                ->where($map)
                ->order('weigh asc')
                ->limit(5)
                ->select();

            $data = addWebSiteUrl($data, ['image']);

            //后续可以设置更长的过期时间
            Cache::set('banner',$data,'60');
        }

        $this->success('i18n:62',$data);
    }
}
